﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Watcher.Data;
using System.IO;
using System.Xml.Serialization;
using Apache.NMS;
using Watcher.AMQ;
using System.Collections;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;

namespace Watcher
{
    public partial class AddServer : Form
    {
        public AddServer()
        {
            InitializeComponent();
            ServerPort.Text = "61616";
        }

        private  string topicName = null;
        private  IConnectionFactory connectionFactory;
        private  IConnection connection;
        private  ISession session;
        private  IMessageProducer producer;


        private void SaveButton_Click(object sender, EventArgs e)
        {
            XmlTextReader xtr = new XmlTextReader("serverlistsource.xml");
            ServerCollection servers = null;
            string path = "serverlistsource.xml";
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "servercollection";

            XmlSerializer serializer = new XmlSerializer(typeof(ServerCollection), xRoot);

            StreamReader reader = new StreamReader(path);
           

            try
            {
                servers = (ServerCollection)serializer.Deserialize(reader);
            }
            catch (Exception w)
            {
                Console.WriteLine("Error: " + w);
            }
            reader.Close();
            List<Server> sl = new List<Server>();
            foreach (Server s in servers._server)
            {
                sl.Add(s);
            }

            Server newServer = new Server();
            newServer.Default = "false";
            newServer.Ip = ServerAddress.Text;
            newServer.Name = ServerName.Text;
            newServer.Port = ServerPort.Text;

            sl.Add(newServer);
            servers = new ServerCollection();
            servers._server = new Server[sl.Count()];
            int i = 0;
            foreach (Server s in sl)
            {
                servers._server[i] = s;
                
                i++;
            }
            StreamWriter writer = new StreamWriter(path);
            serializer.Serialize(writer, servers);
            writer.Close();
            this.Close();
        }

        private void tconnection_Click(object sender, EventArgs e)
        {
            bool ConnectionSuccess = false;
            string brokerUri = "tcp://" + ServerAddress.Text + ":" + ServerPort.Text;
            Serverlink.Text = brokerUri;
            topicName = "simulation";

            if (ServerAddress.Text == null || ServerPort.Text == null)
            {
                
            }
            
            
            try
            {
                connectionFactory = new ConnectionFactory(brokerUri);
                this.connection = this.connectionFactory.CreateConnection();
                ConnectionStatus.Text = "Connection Established";
                ConnectionStatus.ForeColor = Color.Green;
            }
            catch (Exception Ex)
            {
                ConnectionStatus.Text = "Connection could not be established.";
                ConnectionStatus.ForeColor = Color.Red;
                Console.Write(Ex.ToString());
            }
            
            
        }
    }
}
